package com.jiulan.hearthearing.ui.cart.util;

import android.widget.ImageView;

import com.jiulan.hearthearing.R;
import com.jiulan.hearthearing.ui.cart.model.CartModel;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class CartUtil {

    /**
     * 点击全选按钮，改变所有商品选中状态
     */
    public static void selectAll(List<CartModel.DataBean.ValidBean> list, boolean isSelectAll) {
        isSelectAll = !isSelectAll;
        for (int i = 0; i < (list != null ? list.size() : 0); i++) {
            CartModel.DataBean.ValidBean validBean = list.get(i);
            validBean.setSelect(isSelectAll);
        }
    }

    /**
     * 是否都被选中，即全选
     *
     * @param list
     * @return
     */
    private static boolean isSelectAll(List<CartModel.DataBean.ValidBean> list) {
        for (int i = 0; i < (list != null ? list.size() : 0); i++) {
            boolean isSelect = list.get(i).isSelect();
            if (!isSelect) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断是否有商品被选中
     *
     * @param list
     * @return
     */
    public static boolean hasSelectedGoods(List<CartModel.DataBean.ValidBean> list) {
        if (getSelectGoodsIds(list).size() == 0) {
            return false;
        }
        return true;
    }

    /**
     * 勾与不勾选中选项
     *
     * @param isSelect 原先状态
     * @param ivCheck
     * @return 是否勾上，之后状态
     */
    public static boolean checkItem(boolean isSelect, ImageView ivCheck) {
        if (isSelect) {
            ivCheck.setImageResource(R.mipmap.xuanzhong);
        } else {
            ivCheck.setImageResource(R.mipmap.weixuanzhong);
        }
        return isSelect;
    }

    /**
     * @return 选中的商品总价
     */
    public static String getTotalPrice(List<CartModel.DataBean.ValidBean> list) {
        String totalPrice = "";
        double total_price = 0.00;
        for (int i = 0; i < list.size(); i++) {
            CartModel.DataBean.ValidBean goods = list.get(i);
            boolean isSelect = goods.isSelect();
            if (isSelect) {
                int num = goods.getCart_num();
                double price = Double.parseDouble(goods.getTruePrice());
                total_price = total_price + num * price;
            }
        }
        //让Double类型完整显示，不用科学计数法显示大写字母E
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        totalPrice = decimalFormat.format(total_price);
        return totalPrice;
    }

    /**
     * 选择商品主键id
     *
     * @param list
     * @return
     */
    public static List<String> getSelectGoodsIds(List<CartModel.DataBean.ValidBean> list) {
        List<String> ids = new ArrayList<>();
        for (int i = 0; i < (list != null ? list.size() : 0); i++) {
            boolean isSelectd = list.get(i).isSelect();
            if (isSelectd) {
                String id = String.valueOf(list.get(i).getId());
                ids.add(id);
            }
        }
        return ids;
    }

}
